﻿<!--
// ****************************************************************************
// <author>mishkin Ivan</author>
// <email>Mishkin_Ivan@mail.ru</email>
// <date>28.01.2015</date>
// <project>ItemsFilter</project>
// <license> GNU General Public License version 3 (GPLv3) </license>
// ****************************************************************************
-->
<UserControl x:Class="Northwind.NET.Sample.View.OrdersView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:Sample="clr-namespace:Northwind.NET.Sample"
             xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
             xmlns:bsFilter="http://schemas.bolapansoft.com/xaml/Controls/ItemsFilter"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:Northwind.NET.Sample"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:model="clr-namespace:Northwind.NET.EF6Model;assembly=Northwind.NET.EF6Model"
             xmlns:vm="clr-namespace:Northwind.NET.Sample.ViewModel"
             xmlns:vw="clr-namespace:Northwind.NET.Sample.View"
             x:Name="UserControl"
             DataContextChanged="UserControl_DataContextChanged"
             Unloaded="UserControl_Unloaded"
             d:DataContext="{d:DesignInstance Type=vm:OrdersVm,
                                              IsDesignTimeCreatable=True}"
             d:DesignHeight="616.5"
             d:DesignWidth="668.5"
             mc:Ignorable="d">
    <!--    -->

    <UserControl.Resources>
        <vw:TextToImageSourceConverter x:Key="TextToImageSourceConverter" />
        <vw:ProductToProductConverter x:Key="ProductToProductConverter" />
        <vw:ProductToNameConverter x:Key="ProductToNameConverter" />
        <vw:NullToUnsetValueConverter x:Key="NullToUnsetValueConverter" />
        <ControlTemplate x:Key="ProductIDTextBoxTemplate" TargetType="{x:Type TextBox}">
            <Themes:ListBoxChrome x:Name="Bd"
                                  Background="{TemplateBinding Background}"
                                  BorderBrush="{TemplateBinding BorderBrush}"
                                  BorderThickness="{TemplateBinding BorderThickness}"
                                  RenderFocused="{TemplateBinding IsKeyboardFocusWithin}"
                                  RenderMouseOver="{TemplateBinding IsMouseOver}"
                                  SnapsToDevicePixels="True">
                <ComboBox DisplayMemberPath="Name"
                          ItemsSource="{Binding Products,
                                                Source={x:Static local:Workspace.This}}"
                          SelectedItem="{Binding Product,
                                                 UpdateSourceTrigger=PropertyChanged}" />
            </Themes:ListBoxChrome>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

    </UserControl.Resources>

    <Grid x:Name="LayoutRoot">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="EmployeeFilterState">
                <VisualState x:Name="Unfiltered" />
                <VisualState x:Name="Filtered">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="navigator" Storyboard.TargetProperty="(Panel.Background)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource FilterBackground}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="navigator" Storyboard.TargetProperty="(Border.BorderBrush)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource FilterPressedPenColor}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Border Grid.Row="1"
                Margin="0,3,0,0"
                Background="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"
                BorderBrush="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}"
                BorderThickness="1"
                CornerRadius="5">
            <!--  d:DataContext="{d:DesignData Source=../SampleData/Order.xaml}"  -->
            <Grid Margin="5" DataContext="{Binding OrdersCollectionView.CurrentItem}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition MinHeight="345" />
                </Grid.RowDefinitions>
                <Border Width="103"
                        Height="141"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        BorderBrush="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}"
                        BorderThickness="1,1,2,2"
                        CornerRadius="2">
                    <Grid Width="100" Height="137.533">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <TextBlock Margin="5,0"
                                   HorizontalAlignment="Right"
                                   FontSize="13.333"
                                   Text="Employee" />
                        <bsFilter:ColumnFilter Key="Employee"
                                               Grid.Column="1"
                                               ParentCollection="{Binding DataContext.OrdersCollectionView,
                                                                          ElementName=LayoutRoot}">
                            <bsFilter:ColumnFilter.Resources>
                                <DataTemplate DataType="{x:Type model:Employee}">
                                    <TextBlock><Run Text="{Binding FirstName}" /><Run Text=" " /><Run Text="{Binding LastName}" />
                                    </TextBlock>
                                </DataTemplate>
                            </bsFilter:ColumnFilter.Resources>
                        </bsFilter:ColumnFilter>
                        <Image Grid.Row="1"
                               Grid.ColumnSpan="2"
                               Width="100"
                               Height="100"
                               Source="{Binding Employee.Photo,
                                                Converter={StaticResource TextToImageSourceConverter}}" />
                        <TextBlock Grid.Row="2"
                                   Grid.ColumnSpan="2"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Top"
                                   FontFamily="Times New Roman"
                                   FontStyle="Italic"
                                   Text="{Binding Employee.FirstName}" />
                    </Grid>
                </Border>
                <Grid x:Name="partOrderHeader"
                      Grid.Column="1"
                      Grid.ColumnSpan="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Margin="10,5"
                               FontFamily="Times New Roman"
                               FontSize="20"><Run Text="#" /><Run Text=" " /><Run FontWeight="Bold" Text="{Binding ID}" /><Run Text=" " /><Run Text="     " /><Run Text=" " /><Run FontSize="16" Text="{Binding OrderDate}" />
                    </TextBlock>
                    <Border Grid.Row="2"
                            Grid.ColumnSpan="2"
                            Margin="5,20,5,5"
                            Background="{DynamicResource {x:Static SystemColors.InfoBrushKey}}"
                            BorderBrush="{DynamicResource {x:Static SystemColors.MenuHighlightBrushKey}}"
                            BorderThickness="1"
                            CornerRadius="5"
                            Padding="5">
                        <Grid DataContext="{Binding Customer}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <TextBlock Margin="5"
                                       Block.TextAlignment="Center"
                                       FontFamily="Century"
                                       FontSize="16">
                                <!--
                                    <Run Text="{Binding Code}" />
                                    <Run Text="     " />
                                -->
                                <Bold><Run Text="{Binding Name}" />
                                </Bold>
                            </TextBlock>
                            <TextBlock Grid.Row="1"
                                       Margin="10,5"
                                       FontFamily="Century"
                                       FontSize="14"
                                       LineStackingStrategy="BlockLineHeight"><Run Text="{Binding Country}" /><Run Text=" " /><Run Text=",   " /><Run Text=" " /><Run FontSize="16" Text="{Binding City}" />
                                <LineBreak />
                                <Italic><Run FontFamily="Times New Roman" Text="{Binding ContactTitle}" />
                                </Italic><Run Text=" " /><Run Text="  " /><Run Text=" " /><Run Text=" " /><Run Text=" " />
                                <Bold><Run Text="{Binding ContactName}" />
                                </Bold>
                            </TextBlock>

                            <TextBlock Grid.Row="1"
                                       Grid.Column="1"
                                       Margin="10,5"
                                       HorizontalAlignment="Right"
                                       VerticalAlignment="Top"
                                       FontSize="14"><Run Text="tel:" /><Run Text=" " /><Run FontWeight="Bold" Text="{Binding Phone}" />
                            </TextBlock>
                        </Grid>

                    </Border>
                    <Border Grid.Row="1"
                            MinWidth="100"
                            Margin="5,0,20,-5"
                            HorizontalAlignment="Left"
                            VerticalAlignment="Top"
                            Background="{DynamicResource {x:Static SystemColors.InfoBrushKey}}"
                            BorderBrush="{DynamicResource {x:Static SystemColors.MenuHighlightBrushKey}}"
                            BorderThickness="1,1,1,0"
                            CornerRadius="5"
                            Padding="5,0">
                        <StackPanel Height="20"
                                    Margin="3"
                                    HorizontalAlignment="Left"
                                    Orientation="Horizontal">
                            <TextBlock MinWidth="64"
                                       HorizontalAlignment="Left"
                                       FontFamily="Times New Roman"
                                       FontSize="18"
                                       FontStyle="Italic"
                                       Text="{Binding Customer.Code}" />
                            <ComboBox x:Name="customerComboBox"
                                      Width="18"
                                      Height="20"
                                      HorizontalAlignment="Left"
                                      VerticalAlignment="Center"
                                      ItemsSource="{Binding Customers,
                                                            Source={StaticResource Workspace}}"
                                      SelectedItem="{Binding Customer,
                                                             Converter={StaticResource NullToUnsetValueConverter}}"
                                      Style="{DynamicResource ComboBoxStyle}" />
                          
                        </StackPanel>

                    </Border>

                </Grid>
                <DataGrid x:Name="orderDetailsDataGrid"
                          Grid.Row="1"
                          Grid.ColumnSpan="2"
                          AreRowDetailsFrozen="True"
                          AutoGenerateColumns="False"
                          EnableRowVirtualization="True"
                          IsSynchronizedWithCurrentItem="True"
                          ItemsSource="{Binding OrderDetails}"
                          RowDetailsVisibilityMode="VisibleWhenSelected">
                    <DataGrid.Resources>
                        <Style x:Key="EditProductTextBoxStyle" TargetType="{x:Type TextBox}">
                            <Setter Property="BorderThickness" Value="0" />
                            <Setter Property="Padding" Value="0" />
                            <Setter Property="Template" Value="{StaticResource ProductIDTextBoxTemplate}" />
                        </Style>
                        <DataTemplate DataType="{x:Type model:Product}">
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </DataGrid.Resources>

                    <DataGrid.Columns>

                        <DataGridTextColumn Width="Auto"
                                            Binding="{Binding Product,
                                                              Converter={StaticResource ProductToNameConverter}}"
                                            EditingElementStyle="{StaticResource EditProductTextBoxStyle}"
                                            Header="Product" />
                        <DataGridTextColumn x:Name="discountColumn"
                                            Width="SizeToHeader"
                                            Binding="{Binding Discount,
                                                              Mode=TwoWay}"
                                            Header="Discount" />
                        <DataGridTextColumn x:Name="quantityColumn"
                                            Width="SizeToHeader"
                                            Binding="{Binding Quantity,
                                                              Mode=TwoWay}"
                                            Header="Quantity" />
                        <DataGridTextColumn x:Name="unitPriceColumn"
                                            Width="SizeToHeader"
                                            Binding="{Binding UnitPrice}"
                                            Header="Unit Price" />
                    </DataGrid.Columns>
                </DataGrid>
                <Border Grid.Row="1"
                        Grid.Column="2"
                        Margin="5"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        BorderBrush="{DynamicResource TextBoxBorder}"
                        BorderThickness="1">

                    <Grid x:Name="grid1"
                          Margin="5,8,5,5"
                          DataContext="{Binding OrderDetails/Product}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Label Grid.Row="1"
                               Grid.Column="0"
                               Margin="3"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Center"
                               Content="Category ID:" />
                        <TextBox x:Name="categoryIDTextBox"
                                 Grid.Row="1"
                                 Grid.Column="1"
                                 Width="120"
                                 Height="23"
                                 Margin="3"
                                 HorizontalAlignment="Left"
                                 VerticalAlignment="Center"
                                 BorderThickness="1"
                                 IsReadOnly="True"
                                 Text="{Binding CategoryID,
                                                Mode=TwoWay,
                                                NotifyOnValidationError=true,
                                                ValidatesOnExceptions=true}" />
                        <Label Grid.Row="2"
                               Grid.Column="0"
                               Margin="3"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Center"
                               Content="Discontinued:" />
                        <CheckBox x:Name="discontinuedCheckBox"
                                  Grid.Row="2"
                                  Grid.Column="1"
                                  Margin="3"
                                  HorizontalAlignment="Left"
                                  VerticalAlignment="Center"
                                  Content=""
                                  IsChecked="{Binding Discontinued,
                                                      Mode=TwoWay,
                                                      NotifyOnValidationError=true,
                                                      ValidatesOnExceptions=true}"
                                  IsEnabled="False" />
                        <Label Grid.Row="3"
                               Grid.Column="0"
                               Margin="3"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Center"
                               Content="ID:" />
                        <TextBox x:Name="iDTextBox"
                                 Grid.Row="3"
                                 Grid.Column="1"
                                 Width="120"
                                 Height="23"
                                 Margin="3"
                                 HorizontalAlignment="Left"
                                 VerticalAlignment="Center"
                                 IsReadOnly="True"
                                 Text="{Binding ID,
                                                Mode=TwoWay,
                                                NotifyOnValidationError=true,
                                                ValidatesOnExceptions=true}" />
                        <!--
                            <Label Grid.Row="0"
                            Grid.Column="0"
                            Margin="3"
                            HorizontalAlignment="Left"
                            VerticalAlignment="Center"
                            Content="Name:" />
                        -->
                        <TextBox x:Name="nameTextBox"
                                 Grid.Row="0"
                                 Grid.ColumnSpan="2"
                                 Width="Auto"
                                 Height="Auto"
                                 MaxWidth="225"
                                 Margin="3"
                                 HorizontalAlignment="Center"
                                 BorderBrush="{x:Null}"
                                 FontFamily="Times New Roman"
                                 FontSize="18"
                                 FontStyle="Italic"
                                 Foreground="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}"
                                 IsReadOnly="True"
                                 Text="{Binding Name}"
                                 TextWrapping="Wrap" />
                        <Label Grid.Row="4"
                               Grid.Column="0"
                               Margin="3"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Center"
                               Content="Quantity Per Unit:" />
                        <TextBox x:Name="quantityPerUnitTextBox"
                                 Grid.Row="4"
                                 Grid.Column="1"
                                 Width="120"
                                 Height="23"
                                 Margin="3"
                                 HorizontalAlignment="Left"
                                 VerticalAlignment="Center"
                                 IsReadOnly="True"
                                 Text="{Binding QuantityPerUnit,
                                                Mode=TwoWay,
                                                NotifyOnValidationError=true,
                                                ValidatesOnExceptions=true}" />
                        <Label Grid.Row="5"
                               Grid.Column="0"
                               Margin="3"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Center"
                               Content="Reorder Level:" />
                        <TextBox x:Name="reorderLevelTextBox"
                                 Grid.Row="5"
                                 Grid.Column="1"
                                 Width="120"
                                 Height="23"
                                 Margin="3"
                                 HorizontalAlignment="Left"
                                 VerticalAlignment="Center"
                                 IsReadOnly="True"
                                 Text="{Binding ReorderLevel,
                                                Mode=TwoWay,
                                                NotifyOnValidationError=true,
                                                ValidatesOnExceptions=true}" />
                        <Label Grid.Row="6"
                               Grid.Column="0"
                               Margin="3"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Center"
                               Content="Supplier ID:" />
                        <TextBox x:Name="supplierIDTextBox"
                                 Grid.Row="6"
                                 Grid.Column="1"
                                 Width="120"
                                 Height="23"
                                 Margin="3"
                                 HorizontalAlignment="Left"
                                 VerticalAlignment="Center"
                                 IsReadOnly="True"
                                 Text="{Binding SupplierID,
                                                Mode=TwoWay,
                                                NotifyOnValidationError=true,
                                                ValidatesOnExceptions=true}" />
                        <Label Grid.Row="7"
                               Grid.Column="0"
                               Margin="3"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Center"
                               Content="Unit Price:" />
                        <TextBox x:Name="unitPriceTextBox"
                                 Grid.Row="7"
                                 Grid.Column="1"
                                 Width="120"
                                 Height="23"
                                 Margin="3"
                                 HorizontalAlignment="Left"
                                 VerticalAlignment="Center"
                                 IsReadOnly="True"
                                 Text="{Binding UnitPrice,
                                                Mode=TwoWay,
                                                NotifyOnValidationError=true,
                                                ValidatesOnExceptions=true}" />
                        <Label Grid.Row="8"
                               Grid.Column="0"
                               Margin="3"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Center"
                               Content="Units In Stock:" />
                        <TextBox x:Name="unitsInStockTextBox"
                                 Grid.Row="8"
                                 Grid.Column="1"
                                 Width="120"
                                 Height="23"
                                 Margin="3"
                                 HorizontalAlignment="Left"
                                 VerticalAlignment="Center"
                                 IsReadOnly="True"
                                 Text="{Binding UnitsInStock,
                                                Mode=TwoWay,
                                                NotifyOnValidationError=true,
                                                ValidatesOnExceptions=true}" />
                        <Label Grid.Row="9"
                               Grid.Column="0"
                               Margin="3"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Center"
                               Content="Units On Order:" />
                        <TextBox x:Name="unitsOnOrderTextBox"
                                 Grid.Row="9"
                                 Grid.Column="1"
                                 Width="120"
                                 Height="23"
                                 Margin="3"
                                 HorizontalAlignment="Left"
                                 VerticalAlignment="Center"
                                 IsReadOnly="True"
                                 Text="{Binding UnitsOnOrder,
                                                Mode=TwoWay,
                                                NotifyOnValidationError=true,
                                                ValidatesOnExceptions=true}" />
                    </Grid>

                </Border>
            </Grid>
        </Border>
        <TextBlock Grid.ColumnSpan="2"
                   Margin="8"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Top"
                   FontFamily="Times New Roman"
                   FontSize="24"
                   Foreground="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}"
                   Text="Employee orders"
                   TextWrapping="Wrap" />
        <Border x:Name="navigator"
                Grid.Row="2"
                Grid.ColumnSpan="2"
                Margin="8"
                HorizontalAlignment="Center"
                BorderBrush="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
                BorderThickness="1"
                CornerRadius="3">
            <StackPanel Orientation="Horizontal">
                <Button Margin="5"
                        Command="{Binding MoveToFirstCommand}"
                        Padding="5">
                    <Path Data="M8,0 v10 l-8,-5z M16,0 v10 l -8,-5z"
                          Fill="{Binding BorderBrush,
                                         ElementName=navigator}"
                          StrokeThickness="2" />
                </Button>
                <RepeatButton Margin="5"
                              Command="{Binding MoveToPreviousCommand}"
                              Padding="5">
                    <Path Data="M8,0 v10 l-8,-5z"
                          Fill="{Binding BorderBrush,
                                         ElementName=navigator}"
                          StrokeThickness="2" />
                </RepeatButton>
                <Border HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        BorderBrush="#FF707070"
                        BorderThickness="1"
                        CornerRadius="2">
                    <TextBlock HorizontalAlignment="Center"
                               VerticalAlignment="Center"
                               Foreground="{Binding BorderBrush,
                                                    ElementName=navigator}"
                               Padding="4"><Run Text="{Binding OrdersCollectionView.CurrentPosition, Mode=OneWay}" /><Run Text=" " /><Run Text="/" /><Run Text=" " /><Run Text="{Binding OrdersCollectionView.Count, Mode=OneWay}" />
                    </TextBlock>
                </Border>
                <RepeatButton Margin="5"
                              Command="{Binding MoveToNextCommand}"
                              Padding="5">
                    <Path Data="M0,0 v10 l8,-5z"
                          Fill="{Binding BorderBrush,
                                         ElementName=navigator}"
                          StrokeThickness="2" />
                </RepeatButton>
                <Button Margin="5"
                        Command="{Binding MoveToLastCommand}"
                        Padding="5">
                    <Path Data="M0,0v10l8,-5z m8,0 v10 l8,-5z"
                          Fill="{Binding BorderBrush,
                                         ElementName=navigator}"
                          StrokeThickness="2" />
                </Button>

            </StackPanel>

        </Border>

    </Grid>
</UserControl>